﻿#include "DeviceDemoPump.h"
#include <thread>
#include <future>

using namespace DeviceLayer;

DYNAMIC_CREATE(DeviceDemoPump)

DeviceDemoPump::DeviceDemoPump()
{
}

bool DeviceDemoPump::Start()
{
    if (!working)
    {
        working = true;
        startAsyncFuture = std::async(std::launch::async, [&]() -> void {
            rpm = 0;
            for (int i = 0; i < 500; i++)
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                rpm += 10;
                if (working == false)
                    break;
            }
        });
    }
    return true;
}

bool DeviceDemoPump::Stop()
{
    if (working)
    {
        working = false;
        stopAsyncFuture = std::async(std::launch::async, [&]() -> void {
            while (rpm > 0)
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                rpm -= 20;
                if (working == true)
                    break;
            }
            rpm = 0;
        });
    }
    return true;
}

bool DeviceDemoPump::InitFromSettingData(const QString& path)
{
    //必须要调用父类的InitFromSettingData
    bool result = DevicePumpBase::InitFromSettingData(path);
    return result;
}

bool DeviceDemoPump::QueryData()
{
    PutDataToPool("rpm", static_cast<int>(rpm));
    PutDataToPool("working", static_cast<bool>(working));
    return true;
}
